
clear all
set more off, perm
capture log close
set maxvar 32767
global klmChinaReach2: env klmChinaReach2
global datapath "${klmChinaReach2}/Fuyao_Erlfang"
cd "$datapath\eta\maturation"


**# Cognitive


use "$datapath\estimates\maturation\Graphs\c\c_mul_wo_si", clear
keep if var == "Delta"
drop n _merge var var1 
rename level levelc2
merge 1:m levelc2 using eta_maturation_cog_mul

replace mul_pe = 1 if (levelc2 == 1 | levelc2 == 3 | levelc2 == 4 | levelc2 == 9 | levelc2 == 13) & mul_pe == .

replace eta = . if eta == -99

gen delta_eta = mul_pe*eta

gen se = delta_eta

preserve
collapse delta_eta levelc2 ts_id (semean) se, by(indexc2)
gen lb_d_eta = delta_eta - 1.96*se
gen ub_d_eta = delta_eta + 1.96*se

sort levelc2 ts_id
gen indexc2_1= sum(!missing(levelc2)) if !missing(levelc2)

merge 1:1 ts_id using passrate_cog, keepusing(mean_task lb_95 ub_95) 

label var delta_eta "{&delta}(l)E({&eta})"
label var ub_d_eta "95% CI"
label var mean_task "Data"

local xlines ""
local xlabels ""

sum levelc2
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexc2_1 if levelc2==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}
	twoway connected delta_eta indexc2_1, sort lwidth(medthin) msize(small) lpattern(solid) yaxis(1) || line ub_d_eta indexc2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(1) || line lb_d_eta indexc2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(1) || connected mean_task indexc2_1, sort lwidth(medthin) msize(small) lpattern(dash_dot) msymbol(T) yaxis(2) || line ub_95 indexc2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) || line lb_95 indexc2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(medthin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	ytitle({&delta}(l)E({&eta}), size(small) axis(1)) ytitle(Passing Rate, size(small) axis(2)) ///
	xtitle(Difficulty Level, size(small)) ylabel(,labsize(small) axis(1)) ylabel(,labsize(small) axis(2)) ///
	title("{&delta}(l)E({&eta}) and Average Passing Rate of Cognitive Tasks by Level", size(medium)) subtitle(All Tasks, size(small)) legend(order(1 4 2) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("Note: The dashed yellow lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by order of the curriculum design.", size(small) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("cog_delta_eta", replace)
	graph export "cog_delta_eta.pdf", replace	
restore



forval i=1/3{
	gen delta_eta`i' = delta_eta if abi_ct == `i'
	gen se`i' = delta_eta`i'
}

preserve
collapse delta_eta1 delta_eta2 delta_eta3 levelc2 ts_id (semean) se1 se2 se3, by(indexc2)

forval i =1/3{
gen lb`i' = delta_eta`i' - 1.96*se`i'
gen ub`i' = delta_eta`i' + 1.96*se`i'
}
label var delta_eta1 "Fast"
label var delta_eta2 "Normal"
label var delta_eta3 "Slow"
label var ub1 "95% CI"

sort levelc2 ts_id
gen indexc2_1= sum(!missing(levelc2)) if !missing(levelc2)

merge 1:1 ts_id using passrate_cog, keepusing(mean_task lb_95 ub_95) 

label var mean_task "Data"

local xlines ""
local xlabels ""

sum levelc2
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexc2_1 if levelc2==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}

twoway line delta_eta1 indexc2_1, sort lwidth(medthin) yaxis(1) || line ub1 indexc2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb1 indexc2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || connected delta_eta2 indexc2_1, sort msize(small) lwidth(medthin) yaxis(1) || line ub2 indexc2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb2 indexc2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line delta_eta3 indexc2_1, sort lpattern(longdash_shortdash) lwidth(medthin) yaxis(1) || line ub3 indexc2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb3 indexc2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line mean_task indexc2_1, sort lwidth(medium) msize(small) lpattern(dash_dot) msymbol(T) yaxis(2) || line ub_95 indexc2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) || line lb_95 indexc2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(medthin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	ytitle({&delta}(l)E({&eta}), size(small) axis(1)) ytitle(Passing Rate, size(small) axis(2)) ///
	xtitle(Difficulty Level, size(small)) ylabel(,labsize(small) axis(1)) ylabel(,labsize(small) axis(2)) ///
	title("{&delta}(l)E({&eta}) and Average Passing Rate of Cognitive Tasks by Level", size(medium)) subtitle(By Ability Group, size(small)) legend(order(1 4 7 10 2) row(1) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Fast group: the child can pass the first task at over 80% of the difficulty levels, and the average pass rate at that level is greater than 80%." "Normal group: the child doesn't pass the first task, and the pass rate is greater than 50%; or the child passes the first task, and the pass rate is" "between 50% and 80%. Slow group: the average pass rate is less than 50%.    2. 95% confidence intervals are shown for three groups.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("cog_abi_d_eta", replace)
	graph export "cog_abi_d_eta.pdf", replace

restore	


**# language


use "$datapath\estimates\maturation\Graphs\l\l_mul_wo_si", clear
keep if var == "Delta"
drop n var var1 
rename level levell
merge 1:m levell using eta_maturation_lang_mul

replace mul_pe = 1 if levell == 2 & mul_pe == .

replace eta = . if eta == -99

gen delta_eta = mul_pe*eta

gen se = delta_eta

preserve
collapse delta_eta levell ts_id (semean) se, by(indexl)
drop if levell ==1
gen lb_d_eta = delta_eta - 1.96*se
gen ub_d_eta = delta_eta + 1.96*se

sort levell ts_id
gen indexl_1= sum(!missing(levell)) if !missing(levell)

merge 1:1 ts_id using passrate_lang, keepusing(mean_task lb_95 ub_95) 

label var delta_eta "{&delta}(l)E({&eta})"
label var ub_d_eta "95% CI"
label var mean_task "Data"

local xlines ""
local xlabels ""

sum levell
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexl_1 if levell==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}
	twoway line delta_eta indexl_1, sort lwidth(medthin) msize(small) lpattern(solid) yaxis(1) || line ub_d_eta indexl_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(1) || line lb_d_eta indexl_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(1) || connected mean_task indexl_1, sort lwidth(medthin) msize(small) lpattern(dash_dot) msymbol(T) yaxis(2) || line ub_95 indexl_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) || line lb_95 indexl_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(medthin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	ytitle({&delta}(l)E({&eta}), size(small) axis(1)) ytitle(Passing Rate, size(small) axis(2)) ///
	xtitle(Difficulty Level, size(small)) ylabel(,labsize(small) axis(1)) ylabel(,labsize(small) axis(2)) ///
	title("{&delta}(l)E({&eta}) and Average Passing Rate of Language Tasks by Level", size(medium)) subtitle(All Tasks, size(small)) legend(order(1 4 2) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("Note: The dashed yellow lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by order of the curriculum design." "All the children started from level 2 or above upon enrolling.", size(small) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("lang_delta_eta", replace)
	graph export "lang_delta_eta.pdf", replace	
restore



forval i=1/3{
	gen delta_eta`i' = delta_eta if abi_lt == `i'
	gen se`i' = delta_eta`i'
}

preserve
collapse delta_eta1 delta_eta2 delta_eta3 levell ts_id (semean) se1 se2 se3, by(indexl)

forval i =1/3{
gen lb`i' = delta_eta`i' - 1.96*se`i'
gen ub`i' = delta_eta`i' + 1.96*se`i'
}
label var delta_eta1 "Fast"
label var delta_eta2 "Normal"
label var delta_eta3 "Slow"
label var ub1 "95% CI"

sort levell ts_id
gen indexl_1= sum(!missing(levell)) if !missing(levell)

merge 1:1 ts_id using passrate_lang, keepusing(mean_task lb_95 ub_95) 

label var mean_task "Data"

local xlines ""
local xlabels ""

sum levell
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexl_1 if levell==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}

twoway line delta_eta1 indexl_1, sort lwidth(medthin) yaxis(1) || line ub1 indexl_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb1 indexl_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || connected delta_eta2 indexl_1, sort msize(small) lwidth(medthin) yaxis(1) || line ub2 indexl_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb2 indexl_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line delta_eta3 indexl_1, sort lpattern(longdash_shortdash) lwidth(medthin) yaxis(1) || line ub3 indexl_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb3 indexl_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line mean_task indexl_1, sort lwidth(medium) msize(small) lpattern(dash_dot) msymbol(T) yaxis(2) || line ub_95 indexl_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) || line lb_95 indexl_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(medthin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	ytitle({&delta}(l)E({&eta}), size(small) axis(1)) ytitle(Passing Rate, size(small) axis(2)) ///
	xtitle(Difficulty Level, size(small)) ylabel(,labsize(small) axis(1)) ylabel(,labsize(small) axis(2)) ///
	title("{&delta}(l)E({&eta}) and Average Passing Rate of Language Tasks by Level  by Level", size(medium)) subtitle(By Ability Group, size(small))  ///
	legend(order(1 4 7 10 2) row(1) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Fast group: the child can pass the first task at over 80% of the difficulty levels, and the average pass rate at that level is greater than 80%." "Normal group: the child doesn't pass the first task, and the pass rate is greater than 50%; or the child passes the first task, and the pass rate is" "between 50% and 80%. Slow group: the average pass rate is less than 50%.  2. 95% confidence intervals are shown for three groups." "3. All the children started from level 2 or above upon enrolling.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("lang_abi_d_eta", replace)
	graph export "lang_abi_d_eta.pdf", replace

restore	



**# fine


use "$datapath\estimates\maturation\Graphs\f\f_mul_wo_si", clear
keep if var == "Delta"
drop n var var1 
rename level levelf2
merge 1:m levelf2 using eta_maturation_fine_mul

replace mul_pe = 1 if (levelf2 == 1 | levelf2 == 2) & mul_pe == .

replace eta = . if eta == -99

gen delta_eta = mul_pe*eta

gen se = delta_eta

preserve
collapse delta_eta levelf2 ts_id (semean) se, by(indexf2)
gen lb_d_eta = delta_eta - 1.96*se
gen ub_d_eta = delta_eta + 1.96*se

sort levelf2 ts_id
gen indexf2_1= sum(!missing(levelf2)) if !missing(levelf2)

merge 1:1 ts_id using passrate_fine, keepusing(mean_task lb_95 ub_95) 

label var delta_eta "{&delta}(l)E({&eta})"
label var ub_d_eta "95% CI"
label var mean_task "Data"

local xlines ""
local xlabels ""

sum levelf2
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexf2_1 if levelf2==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}
	twoway connected delta_eta indexf2_1, sort lwidth(medthin) msize(small) lpattern(solid) yaxis(1) || line ub_d_eta indexf2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(1) || line lb_d_eta indexf2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(1) || connected mean_task indexf2_1, sort lwidth(medthin) msize(small) lpattern(dash_dot) msymbol(T) yaxis(2) || line ub_95 indexf2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) || line lb_95 indexf2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2)   ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(medthin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	ytitle({&delta}(l)E({&eta}), size(small) axis(1)) ytitle(Passing Rate, size(small) axis(2)) ///
	xtitle(Difficulty Level, size(small)) ylabel(,labsize(small) axis(1)) ylabel(,labsize(small) axis(2)) ///
	title("{&delta}(l)E({&eta}) and Average Passing Rate of Fine Motor Tasks by Level", size(medium)) subtitle(All Tasks, size(small)) legend(order(1 4 2) row(1) size(small) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("Note: The dashed yellow lines indicate the last task at each difficulty level. Within difficulty levels," "tasks are arranged by order of the curriculum design.", size(small) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin))  ///
	saving("fine_delta_eta", replace)
	graph export "fine_delta_eta.pdf", replace	
restore



forval i=1/3{
	gen delta_eta`i' = delta_eta if abi_ft == `i'
	gen se`i' = delta_eta`i'
}

preserve
collapse delta_eta1 delta_eta2 delta_eta3 levelf2 ts_id (semean) se1 se2 se3, by(indexf2)

forval i =1/3{
gen lb`i' = delta_eta`i' - 1.96*se`i'
gen ub`i' = delta_eta`i' + 1.96*se`i'
}
label var delta_eta1 "Fast"
label var delta_eta2 "Normal"
label var delta_eta3 "Slow"
label var ub1 "95% CI"

sort levelf2 ts_id
gen indexf2_1= sum(!missing(levelf2)) if !missing(levelf2)

merge 1:1 ts_id using passrate_fine, keepusing(mean_task lb_95 ub_95) 

label var mean_task "Data"

local xlines ""
local xlabels ""

sum levelf2
if r(N)>0 {
	forval level=`r(min)'/`r(max)' {
		sum indexf2_1 if levelf2==`level'
		if r(N)>0 {
		local xlines "`xlines' `r(max)'"
		local xlabels `" `xlabels' `r(max)' "`level'" "'
		}
	}
}

twoway line delta_eta1 indexf2_1, sort lwidth(medthin) yaxis(1) || line ub1 indexf2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb1 indexf2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || connected delta_eta2 indexf2_1, sort msize(small) lwidth(medthin) yaxis(1) || line ub2 indexf2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb2 indexf2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line delta_eta3 indexf2_1, sort lpattern(longdash_shortdash) lwidth(medthin) yaxis(1) || line ub3 indexf2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line lb3 indexf2_1, sort lcolor(gs15) lpattern(dash) lwidth(medthin) yaxis(1) || line mean_task indexf2_1, sort lwidth(medium) msize(small) lpattern(dash_dot) msymbol(T) yaxis(2) || line ub_95 indexf2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) || line lb_95 indexf2_1, sort lcolor(gs11) lpattern(dash) lwidth(medthin) yaxis(2) ///
	xline(`xlines', lcolor(khaki) lpattern(dash) lwidth(medthin)) ///
	xlabel(`xlabels', labsize(small)) xmtick(, labsize(small)) ///
	ytitle({&delta}(l)E({&eta}), size(small) axis(1)) ytitle(Passing Rate, size(small) axis(2)) ///
	xtitle(Difficulty Level, size(small)) ylabel(,labsize(small) axis(1)) ylabel(,labsize(small) axis(2)) ///
	title("{&delta}(l)E({&eta}) and Average Passing Rate of Fine Motor Tasks by Level by Level", size(medium)) subtitle(By Ability Group, size(small))  ///
	legend(order(1 4 7 10 2) row(1) size(vsmall) nobox fcolor() region(fcolor(white) margin(zero)) bmargin(zero) rowgap(tiny) colgap(tiny) keygap(tiny)) ///
	note("1. Fast group: the child can pass the first task at over 80% of the difficulty levels, and the average pass rate at that level is greater than 80%." "Normal group: the child doesn't pass the first task, and the pass rate is greater than 50%; or the child passes the first task, and the pass rate is" "between 50% and 80%. Slow group: the average pass rate is less than 50%.    2. 95% confidence intervals are shown for three groups.", size(vsmall) span) ///
	graphregion(fcolor(white) ifcolor(white) ilwidth(vvvthin)) plotregion(margin(medsmall) lwidth(vvvthin) ilwidth(vvvthin)) ///
	saving("fine_abi_d_eta", replace)
	graph export "fine_abi_d_eta.pdf", replace

restore	


